package pkg

import (
	"api/appconfig"
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
)

func QiNiuYun(file multipart.File, m *multipart.FileHeader, filename string) (string, error) {

	accessKey := appconfig.Conf.QiNIuYun.AccessKey
	secretKey := appconfig.Conf.QiNIuYun.SecretKey
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := "mjhzg2"
	key := m.Filename
	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	err := uploadManager.UploadReader(context.Background(), file, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
		FileName: key,
	}, nil)

	return "http://ssyl8grwv.hn-bkt.clouddn.com/" + key, err

}
